﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.BLL;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Vote
{
    [Access("258003")]
    public partial class Add : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["ID"] == null)
                {
                    AddUI();
                }
                else
                {
                    EditUI();
                }
            }
        }

        private void AddUI()
        {
            this.UiTitle.InnerText = "添加投票";
        }

        private void EditUI()
        {
            this.UiTitle.InnerText = "修改投票";

            var model = new VoteManage().GetInfo(int.Parse(Request["ID"]));
            this.txtTitle.Text = model.Title;
            this.cbkPass.Checked = model.Pass;

            //cbkSingleSelect.Checked = model.VoteType == 1 ? true : false;
            this.RBL_SingleSelect.SelectedValue = model.VoteType;

            //txtPicUrl.Text = model.PicUrl;
            if (model.EndData != null)
                this.txtEndData.Text = model.EndData.Value.ToShortDateString();
            //txtwindowsize.Text = model.windowsize;
            this.ddlTemplate.SelectedValue = model.TemplateFileName;
            this.cbkLimitIP.Checked = model.LimitIP;

            var modelChoice = new VoteChoiceManage().ForVoteLoad(Request["ID"], list =>
            {
                var sb = new StringBuilder();
                sb.Append("$(document).ready(function (){");
                sb.AppendLine("Row = $('#ChoiceList #trSource');");
                for (var i = 1; i < list.Count; i++)
                {
                    sb.AppendLine("LoadChoice('" + list[i].Id + "','" + list[i].ChoiceTitle + "','" + list[i].VoteNum +
                                  "');");
                }
                sb.AppendLine("});");

                ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, typeof (UpdatePanel), "LoadChoice",
                    sb.ToString(),
                    true);
            });
            if (modelChoice == null) return;

            this.hfID.Value = modelChoice.Id.ToString();
            this.txtChoiceTitle.Text = modelChoice.ChoiceTitle;
            this.NtxtVoteNum.Text = modelChoice.VoteNum.ToString();
        }

        protected void btnAddChoice_Click(object sender, EventArgs e)
        {
            var modelvote = new WinnerCMS.Model.Vote();
            if (Request["ID"] == null)
            {
                modelvote.Id = 0;
            }
            else
            {
                modelvote.Id = int.Parse(Request["ID"]);
            }
            modelvote.Title = this.txtTitle.Text;
            modelvote.VoteType = this.RBL_SingleSelect.SelectedValue;
            modelvote.Pass = this.cbkPass.Checked;

            DateTime endData;
            if (DateTime.TryParse(this.txtEndData.Text, out endData))
            {
                modelvote.EndData = endData;
            }

            modelvote.LimitIP = this.cbkLimitIP.Checked;
            modelvote.TemplateFileName = this.ddlTemplate.SelectedValue;

            var resu = new VoteManage().Save(modelvote,
                Request.Form["hfID"],
                Request.Form["txtChoiceTitle"],
                Request.Form["NtxtVoteNum"]);

            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this.UpdatePanel1);
            }
            else
            {
                Response.Redirect("Manage.aspx");
            }
        }
    }
}